/*
 * File:    DocumentParser.java
 * Created: 05-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2009, Bitgate Mobile, LLC.  All Rights Reserved.
 *
 * software@bitgatemobile.com
 */

package com.webplasm.render.engine;

import static com.bitgate.util.debug.Debug.debug;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This class loads in a document and parses the document contents using the <code>DocumentBuilderPool</code> class.
 * Once the document is parsed, the document's root node then is made available.  Otherwise, the class will
 * throw an appropriate exception indicating a problem that occurred.
 * 
 * @author kenji
 * @since Webplasm 2.0
 */
public class DocumentParser {
	private Node rootNode;
	
	/**
	 * Parses the document given.  The document must be an XML document, and it must be well-formed.
	 * 
	 * @param documentData <code>byte[]</code> array containing the data to parse.
	 * @throws SAXException on any SAX parsing errors
	 * @throws IOException if an I/O problem occurs
	 * @throws IllegalArgumentException on any other problems.
	 */
	public DocumentParser(byte[] documentData) throws SAXException, IOException, IllegalArgumentException {
		Document doc = null;
		
		rootNode = null;
		doc = DocumentBuilderPool.getDefault().getBuilder().parse(new InputSource(new ByteArrayInputStream(documentData)));
		debug("Document parse prepared.");
		
		rootNode = doc.getDocumentElement();
	}
	
	/**
	 * Returns the root node object after parsing.
	 * 
	 * @return <code>Node</code> object containing the root node, <code>null</code> if the object failed to parse.
	 */
	public Node getRootNode() {
		return rootNode;
	}
}